# -*- coding: utf-8 -*-
import scrapy

"""
正统模拟登录方法：
首先发送登录页面的get请求，获取到页面里的登录必须的参数，比如token
然后和账户密码一起post到服务器，登录成功
"""

class Login3Spider(scrapy.Spider):
    name = 'login3'
    allowed_domains = ['www.renren.com']
    start_urls = ['http://www.renren.com/PLogin.do']

    def parse(self, response):
        # _xsrf = response.xpath("//_xsrf").extract()[0]
        yield scrapy.FormRequest.from_response(
            response,
            formdata={"email": "13179143428", "password": "hanjinda1122"},
            callback=self.parse_page
        )
    def parse_page(self, response):
        print(response.url)
        url = "http://www.renren.com/880151247/profile"  # 大鹏的人人主页
        yield scrapy.Request(url, callback=self.parse_newpage)

    def parse_newpage(self, response):
        print(response.body.decode("utf-8"))
        with open("login3.html", "w", encoding='utf8') as f:
            f.write(response.body.decode("utf-8"))